﻿<%@ WebHandler Language="C#" Class="ebayServices" %>

using System;
using System.Web;
using ebay.Dal.CenterDB_EbayTableAdapters;
using ebay.BLL;
using ebay.Model;
using System.Collections.Generic;
using System.Web.SessionState;
using System.Data;
using ebay.ebayApi;
using System.Configuration;

public class ebayServices : IHttpHandler, IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {

        string act = context.Request["act"];
        string cmd = context.Request["tname"];
        string retMsg = "";
        string retCode = "1";
        string sessionId = "";
        string loginUrl = "";
        string token = "";
        if (context.Session["UserId"] == null)
        {
            retCode = "1001";
            retMsg = "请先登陆";
            act = "";
        }

        if (!string.IsNullOrEmpty(act) && !string.IsNullOrEmpty(cmd))
        {
            act = WebUtility.InputText(act, 20);
            cmd = WebUtility.InputText(cmd, 80);
            switch (act)
            {
                case "sync":
                    switch (cmd)
                    {
                        case "Category":
                            {
                                bool result = SyncCategory();
                                if (result)
                                {
                                    retCode = "0";
                                }
                                else
                                {
                                    retCode = "1";
                                    retMsg = "Category同步失败";
                                }
                            }
                            break;
                        case "Category2":
                            {
                                bool result = SyncCategory();
                                if (result)
                                {
                                    retCode = "0";
                                }
                                else
                                {
                                    retCode = "1";
                                    retMsg = "Category2同步失败";
                                }
                            }
                            break;
                        case "Condition":
                            {
                                bool result = SyncCategory();
                                if (result)
                                {
                                    retCode = "0";
                                }
                                else
                                {
                                    retCode = "1";
                                    retMsg = "Condition";
                                }
                            }
                            break;

                    }
                    break;
                case "getsessionid":
                    {
                        string rulName = ConfigurationManager.AppSettings["RuName"];
                        ebayCallsApi apiCall = new ebayCallsApi(null);
                        try
                        {
                            sessionId = apiCall.GetSessionId(rulName);
                            retCode = "0";
                            loginUrl = ConfigurationManager.AppSettings["Environment.SignInUrl"] + "&RuName=" + rulName + "&SessID=" + sessionId;
                        }
                        catch (Exception ex)
                        {
                            Log.WriteLog(ex.ToString());
                            retCode = "1";
                            retMsg = "获取SessionId失败";
                        }
                    }
                    break;
                case "fetchtoken":
                    {
                        string sessionid = context.Request["sessionid"];
                        ebayCallsApi apiCall = new ebayCallsApi(null);
                        try
                        {
                            token = apiCall.GetToken(sessionid);
                            retCode = "0";
                        }
                        catch (Exception ex)
                        {
                            Log.WriteLog(ex.ToString());
                            retCode = "1";
                            retMsg = ex.Message;
                        }
                    }
                    break;
            }
        }
        else {
            retCode = "1";
            retMsg = "参数不正确";
        }
        string retrunMsg = "[{\"retCode\":\"" + retCode + "\",\"retMsg\":\"" + retMsg + "\"";
        if (!string.IsNullOrEmpty(sessionId))
        {
            retrunMsg += ",\"sessionId\":\"" + sessionId + "\"";
        }
        if (!string.IsNullOrEmpty(loginUrl))
        {
            retrunMsg += ",\"loginUrl\":\"" + loginUrl + "\"";
        }
        if (!string.IsNullOrEmpty(token))
        {
            retrunMsg += ",\"token\":\"" + token + "\"";
        }
        retrunMsg += "}]";
        context.Response.Write(retrunMsg);
    }

    bool SyncCategory()
    {
        ebay.BLL.Category category = new ebay.BLL.Category();
        try
        {
            T_EbayCategoryTableAdapter adapter = new T_EbayCategoryTableAdapter();

            DataTable table = adapter.GetDataByCategoryTypeId(1);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                table.Rows[i].Delete();
            }
            List<ebay.Model.Category> list = category.GetCategory();
            foreach (ebay.Model.Category m in list)
            {
                table.Rows.Add(m.CategoryId, m.CategoryName, m.CategoryParentID, m.CategoryLevel, m.CategoryParentName, m.CategoryTypeId);
            }
            try
            {
                int val = adapter.Update(table.Select(null, null, System.Data.DataViewRowState.Deleted));
                val = adapter.Update(table.Select(null, null, System.Data.DataViewRowState.Added));
                return true;
            }
            catch(Exception ex)
            {
                Log.WriteLog(ex.ToString());
                return false;
            }
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex.ToString());
            return false;
        }
    }
    bool SyncCategory2()
    {
        ebay.BLL.Category category = new ebay.BLL.Category();
        try
        {
            T_EbayCategoryTableAdapter adapter = new T_EbayCategoryTableAdapter();
            DataTable table = adapter.GetDataByCategoryTypeId(2);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                table.Rows[i].Delete();
            }
            List<ebay.Model.Category> list = category.GetCategory2CS();
            foreach (ebay.Model.Category m in list)
            {
                table.Rows.Add(m.CategoryId, m.CategoryName, m.CategoryParentID, m.CategoryLevel, m.CategoryParentName, m.CategoryTypeId);
            }
            try
            {
                int val = adapter.Update(table.Select(null, null, System.Data.DataViewRowState.Deleted));
                val = adapter.Update(table.Select(null, null, System.Data.DataViewRowState.Added));
                return true;
            }
            catch(Exception ex)
            {
                Log.WriteLog(ex.ToString());
                return false;
            }
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex.ToString());
            return false;
        }
    }
    bool SyncCondition()
    {
        Contidion condtion = new Contidion();
        try
        {
            T_EbayConditionTableAdapter adapter = new T_EbayConditionTableAdapter();
            DataTable table = adapter.GetData();
            for (int i = 0; i < table.Rows.Count; i++)
            {
                table.Rows[i].Delete();
            }
            List<Condition> list = condtion.GetConditionList();
            foreach (Condition m in list)
            {
                table.Rows.Add(m.ConditionId, m.ConditionName);
            }
            try
            {
                int val = adapter.Update(table.Select(null, null, System.Data.DataViewRowState.Deleted));
                val = adapter.Update(table.Select(null, null, System.Data.DataViewRowState.Added));
                return true;
            }
            catch(Exception ex)
            {
                Log.WriteLog(ex.ToString());
                return false;
            }
        }
        catch (Exception ex)
        {
            Log.WriteLog(ex.ToString());
            return false;
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}